﻿using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using SignalRHub.Host;

namespace SignalRHub.Hubs
{
    public class PerformanceVerificationHub : Hub
    {
        private readonly ILogger<PerformanceVerificationHub> _logger;
        private readonly PerformanceVerificationHost _performanceVerificationHost;
        private readonly CancellationToken _cancellationToken;

        public PerformanceVerificationHub(ILogger<PerformanceVerificationHub> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 发送性能检验、标定  统计信息
        /// </summary>
        /// <param name="user"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendAutoStatResult(string notifyVerif)
        {
            await Clients.All.SendAsync("ReceiveAutoStatResult", JsonConvert.SerializeObject(notifyVerif));
        }

        /// <summary>
        /// 发送完整性能检验、标定 最终结果信息
        /// </summary>
        /// <param name="user"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendVerifCalibResult(string user, string message)
        {
            await Clients.All.SendAsync("GetVerifCalibResult", user, message);

            await this.StopMyBackgroundService();
        }

        /// <summary>
        /// 停止后台服务
        /// </summary>
        /// <returns></returns>
        public async Task StopMyBackgroundService()
        {
            await _performanceVerificationHost.StopAsync(_cancellationToken);
        }
    }
}